package com.ijianji.module_zhuanzhu.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import androidx.cardview.widget.CardView;

import com.fenghuajueli.libbasecoreui.listener.OnListClickListener;
import com.ijianji.module_zhuanzhu.R;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.List;

public class ZhuanZhuCategoryTabAdapter extends CommonNavigatorAdapter {

    private List<String> tabTitleList;
    private OnListClickListener<Integer> onListClickListener;
    private MagicIndicator magicIndicator;
    private int unselectColor = Color.parseColor("#AFB5C2");
    private int selectColor = Color.parseColor("#4C5473");
    private IPagerIndicator iPagerIndicator;

    public ZhuanZhuCategoryTabAdapter(MagicIndicator magicIndicator, List<String> tabTitleList) {
        this.tabTitleList = tabTitleList;
        this.magicIndicator = magicIndicator;
    }

    public void setOnListClickListener(OnListClickListener<Integer> onListClickListener) {
        this.onListClickListener = onListClickListener;
    }

    @Override
    public int getCount() {
        return tabTitleList.size();
    }

    public void setUnselectColor(int unselectColor) {
        this.unselectColor = unselectColor;
    }

    public void setSelectColor(int selectColor) {
        this.selectColor = selectColor;
    }

    public void setiPagerIndicator(IPagerIndicator iPagerIndicator) {
        this.iPagerIndicator = iPagerIndicator;
    }

    @Override
    public IPagerTitleView getTitleView(Context context, int index) {

        CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);
        commonPagerTitleView.setContentView(R.layout.zhuanzhu_category_tab_item_layout);
        TextView tvTitle = commonPagerTitleView.findViewById(R.id.tvTitle);
        CardView cvLine = commonPagerTitleView.findViewById(R.id.cvLine);
        tvTitle.setText(tabTitleList.get(index));
        commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {
            @Override
            public void onSelected(int index, int totalCount) {
                cvLine.setVisibility(View.VISIBLE);
                tvTitle.setTextColor(selectColor);
            }

            @Override
            public void onDeselected(int index, int totalCount) {
                cvLine.setVisibility(View.GONE);
                tvTitle.setTextColor(unselectColor);
            }

            @Override
            public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {

            }

            @Override
            public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {

            }
        });
        commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                magicIndicator.onPageSelected(index);
                magicIndicator.onPageScrolled(index, 0.0F, 0);
                if (onListClickListener != null) {
                    onListClickListener.itemClick(index, index);
                }
            }
        });
        return commonPagerTitleView;
    }

    @Override
    public IPagerIndicator getIndicator(Context context) {
        return null;
    }
}
